CKEDITOR.plugins.add( 'readmore', {
	onLoad: function() {
		var cssStyles = [
			'{',
				'background: url(' + CKEDITOR.getUrl( this.path + 'images/readmore.gif' ) + ') no-repeat center;',
				'clear: both;',
				'width: 100%; _width: 99.9%;',
				'border-top: #999999 1px dotted;',
				'padding:0;',
				'height: 10px;',
				'cursor: default;',
			'}'
			].join( '' ); // Increase specificity to override other styles, e.g. block outline.

		// Add the style that renders our placeholder.
		CKEDITOR.addCss( 'div.readmore' + cssStyles );
	},
	init: function( editor ) {
		if ( editor.blockless ) return;
		// Register the command.
		editor.addCommand( 'readmore', CKEDITOR.plugins.readmoreCmd );
		// Register the toolbar button.
		editor.ui.addButton && editor.ui.addButton( 'readmore', {
			label: 'Readmore',
			command: 'readmore',
			toolbar: 'insert,70',
			icon: this.path + 'images/readmore.png'
		});
		// Opera needs help to select the page-break.
		CKEDITOR.env.opera && editor.on( 'contentDom', function() {
			editor.document.on( 'click', function( evt ) {
				var target = evt.data.getTarget();
				if ( target.is( 'div' ) && target.hasClass( 'readmore' ) )
					editor.getSelection().selectElement( target );
			});
		});
	},

});
// TODO Much probably there's no need to expose this object as public object.
CKEDITOR.plugins.readmoreCmd = {
	exec: function( editor ) {
		var divmore = editor.document.getElementsByTag('div');
		for (var i = 0, len = divmore.count() ; i < len ; i++ ) {
			var dmore = divmore.getItem(i);
			if (dmore.hasClass('readmore')) {
				var msg = 'The document already contains a more.\nDo you want to proceed by removing it first?'
				if (confirm(msg)) {
					dmore.remove();
					break;
				} else {
					return;
				}
			}
		}
		// Create read-only element that represents a print break.
		var pagebreak = CKEDITOR.dom.element.createFromHtml( '<div class="readmore"></div>', editor.document );
		editor.insertElement( pagebreak );
	},
	context: 'div',
};
